#include <bits/stdc++.h>
// 2025/02/01
// tag: 
// Author: Zhang Muen
using namespace std;

int n;
pair<int, int> cows[50001];

unordered_set<int> ids;

signed main()
{
    cin >> n;
    for (int i = 1; i <= n; i++){
        cin >> cows[i].first >> cows[i].second;
        ids.insert(cows[i].second);
    }
    sort(cows + 1, cows + n + 1);
    int cnt = ids.size(), l = 1, r = 1, ans = INT_MAX;
    unordered_map<int, int> mp;
    while (r <= n){
        mp[cows[r].second]++;
        
        while (l < r && mp.size() >= cnt)
        {
            ans = min(ans, cows[r].first - cows[l].first);
            mp[cows[l].second]--;
            if (mp[cows[l].second] == 0)
                mp.erase(cows[l].second);
            l++;
        }
        r++;
    }
    cout << ans;
    return 0;
}